// pages/specialStudy/specialStudy.js
import SpecialApi from '../../api/special.js'
import {config} from '../../config.js'

Page({

    /**
     * 页面的初始数据
     */
    data: {
        show: false,
        headerFlag: false,
        specialType: 'toubu',
        specialId: 'b5940456dcda3062559b2f3bbd439957',
        specialIds: [],
        menuData: [],
        menuIndexOne: 0,
        menuIndexTwo: 0,
        menuIndexThree: 0,
        dataInfo: [],
        title: '',
        letterScrolltop:0,
        loading:false,
        resource_url: config.resource_url,
        modelCategory:0,
        firstMenuName:'',
        letterScrollTwoTop:0
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        this.getMenuList('toubu')
        if (options.id) {
            this.setData({
                specialId: options.id
            })
        }
        this.getSpecialDetails(this.data.specialId)
        this.querySpTeachingThirdMenusList()
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {
        // 页面首次渲染完毕时执行
        let height = wx.getSystemInfoSync().windowHeight
        let $navBar = this.selectComponent("#navBar")
        let navBarHeight = $navBar.data.navBarExtendHeight + $navBar.data.navBarHeight
        let viewHeight = height - navBarHeight
        this.setData({
            viewHeight: viewHeight
        })
    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    },
    showPopup() {
        this.setData({show: true});
    },

    onClose() {
        this.setData({show: false});
    },
    /**
     * 头部返回
     */
    handlerGobackClick() {
        wx.reLaunch({
            url: '/pages/home/home'
        });
    },
    /**
     * 滑动事件
     */
    scrollChange(e) {
        const scrollTop = e.detail.scrollTop
        if (scrollTop >= 20) {
            this.setData({
                headerFlag: true
            })
        } else {
            this.setData({
                headerFlag: false
            })
        }
    },
    /**
     * 搜索
     */
    handlerGoSearchClick() {
        // wx.navigateTo({
        //     url: '/pages/searchSpecial/searchSpecial'
        // });
        let url = '/pages/searchSpecial/searchSpecial'
        if (getCurrentPages().length >= 10) {//判断当前页面栈是否大于等于10.如果大于或等于就使用wx.redirectTo来跳转页面，清除当前页面栈
            wx.redirectTo({url})
        } else {
            wx.navigateTo({url})
        }
    },
    /**
     * 点击专项菜单
     */
    specialTypeClick(e) {
        console.log('e', e)
        const type = e.currentTarget.dataset.index
        this.setData({
            specialType: type,
            letterScrollTwoTop:0,
            menuIndexOne: 0,
            menuIndexTwo: 0,
            menuIndexThree: 0,
        })
        this.getMenuList(type)
    },
    /**
     * 查询专项菜单
     */
    async getMenuList(type) {
        const param = {
            id: type
        }
        const res = await SpecialApi.getMenuList(param)
        console.log(res)
        this.setData({
            menuData: res.data
        })
    },
    /**
     * 查询专项详情数据
     */
    async getSpecialDetails(id) {
        const param = {
            id: id
        }
        this.setData({
            loading:true
        })
        const {data} = await SpecialApi.getSpecialDetails(param)
        this.setData({
            title: data.name,
            dataInfo: JSON.parse(data.bodyStr),
            letterScrolltop:0,
            loading : false,
            modelCategory:data.modelCategory,
            firstMenuName:data.firstMenuName,
            specialId:id
        })
    },
    /**
     * 图片放大预览
     */
    previewImg(event) {
        let currentUrl = event.currentTarget.dataset.src
        wx.previewImage({
            current: this.data.resource_url + currentUrl,
            urls: [this.data.resource_url + currentUrl]
        })
    },
    /**
     * 点击菜单一级
     */
    menuOneClick(e) {
        const index = e.currentTarget.dataset.index
        this.setData({
            menuIndexOne: index,
            letterScrollTwoTop:0,
            menuIndexTwo: 0,
            menuIndexThree: 0,
        })
    },
    /**
     * 点击菜单二级
     */
    menuTwoClick(e) {
        const index = e.currentTarget.dataset.index
        this.setData({
            menuIndexTwo: index,
            menuIndexThree: 0,
            letterScrollTwoTop:0
        })
        //设置滚动到最顶端
    },
    /**
     * 点击菜单三级
     */
    menuThreeClick(e) {
        const index = e.currentTarget.dataset.index
        const id = e.currentTarget.dataset.id
        this.setData({
            menuIndexThree: index,
            specialId: id,
            show: false
        })
        this.getSpecialDetails(id)
    },
    /**
     * 查询专项三级
     */
    async querySpTeachingThirdMenusList() {
        const res = await SpecialApi.spTeachingThirdMenusList()
        const list = res.data
        const specialIds = list.map(item => {
            return item.id
        })
        this.setData({
            specialIds
        })
    },
    /**
     * 上一节、下一节方法
     */
    nextSpecial() {
        const id = this.data.specialId
        const specialIds = this.data.specialIds
        const index = specialIds.findIndex(item => item == id)
        let specialId = ''
        if(index == specialIds.length - 1 ){
            wx.showToast({
                title: '已经是最后一个了',
                icon: 'none',
                duration: 2000
            })
            return
        }
        specialId = specialIds[index + 1]
        this.setData({
            specialId
        })
        this.getSpecialDetails(this.data.specialId)
    },
    /**
     * 上一节
     */
    lastSpecial(){
        const id = this.data.specialId
        const specialIds = this.data.specialIds
        const index = specialIds.findIndex(item => item == id)
        let specialId = ''
        if(index == 0){
            wx.showToast({
                title: '已经是第一个了',
                icon: 'none',
                duration: 2000
            })
            return
        }
        specialId = specialIds[index - 1]
        this.setData({
            specialId
        })
        this.getSpecialDetails(this.data.specialId)
    },
    /**
     * 去听诊
     */
    goToSound(){
        wx.navigateTo({
            url: `/pages/specialSound/specialSound?name=${this.data.title}&firstMenuName=${this.data.firstMenuName}`
        });
    }

})
